package com.mk.seckill.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Author hhwu
 * @Description //TODO MVC配置类
 * @Date 13:28 2022/6/7
 **/
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

	@Autowired
	private UserArgumentResolver userArgumentResolver;
	@Autowired
	private AccessLimitInterceptor accessLimitInterceptor;

	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
		/**
		 * @Author hhwu
		 * @Description //TODO 相当于向容器中添加对象，添加的这些对象可以和controller中的model一样被使用
		 * @Date 13:47 2022/6/7
		 * @Param [resolvers]
		 * @return void
		 **/
		// 添加判断用户存在与否的规则：userArgumentResolver
		resolvers.add(userArgumentResolver);// 从redis中返回ticket对应的user对象。这个东西返回的对象（user）可以直接在controller方法的参数中调用
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		/**
		 * @Author hhwu
		 * @Description //TODO 因为这里继承了WebMvcConfigurer，会覆盖掉properties文件中的配置，因此需指定静态资源路径
		 * @Date 15:47 2022/6/7
		 * @Param [registry]
		 * @return void
		 **/
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		/**
		 * @Author hhwu
		 * @Description //TODO 添加接口限流拦截器
		 * @Date 20:48 2022/6/13
		 * @Param [registry]
		 * @return void
		 **/
		registry.addInterceptor(accessLimitInterceptor);
	}
}